package com.curd.tk.base.service.impl.tkpackaging;

import com.curd.tk.base.dao.IBaseMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;

public class PackagingServiceImpl<DOMAIN, ID, M extends IBaseMapper<DOMAIN>> implements PackagingService<DOMAIN, ID> {
    @Autowired
    protected M mapper;


    /**
     * 当前泛型真实类型的Class
     */
    private Class<DOMAIN> modelClass;

    public PackagingServiceImpl() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        modelClass = (Class<DOMAIN>) pt.getActualTypeArguments()[0];
    }

    /**
     * 持久化
     *
     * @param entity
     */
    @Override
    public int save(DOMAIN entity) {
        return mapper.insertSelective(entity);
    }

    /**
     * 批量持久化
     *
     * @param entitys
     */
    @Override
    public int save(List<DOMAIN> entitys) {
        return mapper.insertList(entitys);
    }


    /**
     * 通过主鍵刪除
     *
     * @param id
     */
    @Override
    public int deleteById(ID id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteAll() {
        int count = 0;
        for (DOMAIN domain : mapper.selectAll()){
            count += mapper.delete(domain);
        }
        return count;
    }

    /**
     * 批量刪除 eg：ids -> “1,2,3,4”
     *
     * @param ids
     */
    @Override
    public int deleteByIds(ID []ids) {
        int count = 0;
        for (ID id : ids){
            count += mapper.deleteByPrimaryKey(id);
        }
        return count;
//        String[] idsArr = ids.split(",");
//        if (idsArr.length == 1) {
//            return mapper.deleteByPrimaryKey(Long.parseLong(ids));
//        } else {
//            int n = 0;
//            for (String id : idsArr) {
//                n += mapper.deleteByPrimaryKey(Long.parseLong(id));
//            }
//            return n;
//        }
    }

    /**
     * 更新
     *
     * @param model
     */
    @Override
    public int update(DOMAIN model) {
        return mapper.updateByPrimaryKeySelective(model);
    }

    /**
     * 通过ID查找
     *
     * @param id
     * @return
     */
    @Override
    public DOMAIN findById(Object id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     * 通过Model中某个成员变量名称（非数据表中column的名称）查找,value需符合unique约束
     *
     * @param fieldName
     * @param value
     * @return
     * @throws Exception
     */
    @Override
    public DOMAIN findBy(String fieldName, Object value) throws Exception {
        try {
            DOMAIN model = modelClass.newInstance();
            Field field = modelClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(model, value);
            return mapper.selectOne(model);
        } catch (ReflectiveOperationException e) {
            throw new Exception(e.getMessage(), e);
        }
    }

    /**
     * 通过多个ID查找//eg：ids -> “1,2,3,4”
     *
     * @param ids
     * @return
     */
    @Override
    public List<DOMAIN> findByIds(String ids) {
        return mapper.selectByIds(ids);
    }

    /**
     * 根据条件查找
     *
     * @param condition
     * @return
     */
    @Override
    public List<DOMAIN> findByCondition(Condition condition) {
        return mapper.selectByCondition(condition);
    }

    /**
     * //获取所有
     *
     * @return
     */
    @Override
    public List<DOMAIN> findAll() {
        return mapper.selectAll();
    }

    /**
     * GitHub 分页插件
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<DOMAIN> findByPage(int page, int size) {
        return this.findByPage(PageHelper.startPage(page,size));
    }

    /**
     * GitHub 分页插件
     * @param page
     * @return
     */
    @Override
    public PageInfo<DOMAIN> findByPage(Page<DOMAIN> page) {
        return new PageInfo<>(mapper.selectAll());
    }
}
